﻿namespace CpuUsageAPI
{
    using System;
    using System.Threading;
    using System.Windows.Forms;

    public class ProcessData
    {
        public string CpuUsage;
        public uint ID;
        public int Index;
        public string Name;
        private long OldKernelTime;
        private DateTime OldUpdate;
        private long OldUserTime;
        public ListViewItem ProcessItem;

        public ProcessData(uint ID, string Name, long OldUserTime, long OldKernelTime)
        {
            this.ID = ID;
            this.Name = Name;
            this.OldUserTime = OldUserTime;
            this.OldKernelTime = OldKernelTime;
            this.OldUpdate = DateTime.Now;
        }

        public int UpdateCpuUsage(long NewUserTime, long NewKernelTime)
        {
            long num2 = NewUserTime - this.OldUserTime;
            long num3 = NewKernelTime - this.OldKernelTime;
            if (DateTime.Now.Ticks == this.OldUpdate.Ticks)
            {
                Thread.Sleep(100);
            }
            long num = DateTime.Now.Ticks - this.OldUpdate.Ticks;
            int num4 = (int) (((num2 + num3) * 100L) / num);
            this.CpuUsage = num4 + "%";
            this.OldUserTime = NewUserTime;
            this.OldKernelTime = NewKernelTime;
            this.OldUpdate = DateTime.Now;
            if (this.ProcessItem.SubItems[2].Text != this.CpuUsage)
            {
                this.ProcessItem.SubItems[2].Text = this.CpuUsage;
            }
            return num4;
        }
    }
}

